from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger


def page_serializer_info(info, list_num, page_num, serializer_class):
    p = Paginator(info, list_num)
    try:
        contract = p.page(page_num).object_list
        _data = serializer_class(contract, many=True).data
    except PageNotAnInteger:
        contract = p.page(1).object_list
        _data = serializer_class(contract, many=True).data
    except EmptyPage:
        _data = []

    something = {
        'pageNo': page_num,
        'pageSize': list_num,
        'list': _data,
        'total': p.count,
    }
    return something



def page_info(info, list_num, page_num):
    p = Paginator(info, list_num)
    try:
        contract = p.page(page_num).object_list
    except PageNotAnInteger:
        contract = p.page(1).object_list
    except EmptyPage:
        contract = "page dont exist!"
    something = {
        'pageNo': page_num,
        'pageSize': list_num,
        'list': contract,
        'total': p.count,
    }
    return something